﻿Imports MyDLL

Public Class Form7

    Dim Nav As New MyDLL.RecordNav

    Private Sub Add()
        Dim Items As Double = TextBox4.Text
        Dim Add As Double = TextBox10.Text
        Dim Ans As Double

        Ans = Items + Add
        TextBox4.Text = Ans
    End Sub

    Private Sub Remove()
        Dim Items As Double = TextBox4.Text
        Dim Remove As Double = TextBox11.Text
        Dim Ans As Double

        Ans = Items - Remove
        TextBox4.Text = Ans
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Try
            Add()
            Remove()
        Catch ex As Exception
            MsgBox("Please insert the needed values")
        End Try
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Try
            Dim Items As Double = TextBox4.Text
            Dim Price As Double = TextBox5.Text
            Dim Ans As Double

            Ans = Items * Price
            TextBox9.Text = Ans
        Catch ex As Exception
            MsgBox("Please insert the needed values")
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim C As String = TextBox7.Text
        Dim Item As String = TextBox1.Text

        If CheckBox1.Checked Then
            Dim Itemxml As XElement = _
            <Item><%= Item %></Item>

            Form1.RichTextBox1.Text = Form1.RichTextBox1.Text & "<Stock>" & vbCr _
                                      & "<Type>Stock Taking</Type>" & vbCr & Itemxml.ToString & vbCr
        End If

        If CheckBox2.Checked Then
            Dim Itemxaml As XElement = _
            <Paragraph FontFamily="Arial" FontSize="20pt">
                Stock Taking: Transactions for <%= Item %>
            </Paragraph>

            Form1.RichTextBox2.Text = Form1.RichTextBox2.Text & _
            "<FlowDocument xmlns=" & C & "http://schemas.microsoft.com/winfx/2006/xaml/presentation" & C & ">" & vbCr & Itemxaml.ToString & vbCr
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim Count As String = TextBox6.Text

        If CheckBox1.Checked Then
            Dim Counter As XElement = _
            <Counter>
                <Count><%= Count %></Count>
            </Counter>

            Form1.RichTextBox1.Text = Form1.RichTextBox1.Text & Counter.ToString & vbCr & "</Stock>"
        End If

        If CheckBox2.Checked Then
            Form1.RichTextBox2.Text = Form1.RichTextBox2.Text & "</FlowDocument>"
        End If
    End Sub
    Private Sub WriteData(ByVal ID As Integer)
        'Dim ID As Integer = TextBox6.Text
        Dim Dat As String = TextBox2.Text
        Dim Person As String = TextBox3.Text
        Dim BalanceI As String = TextBox4.Text
        Dim Price As String = TextBox5.Text
        Dim BalanceM As String = TextBox9.Text
        Dim Added As String = TextBox10.Text
        Dim Removed As String = TextBox11.Text

        If CheckBox3.Checked Then
            ID = ID + 1
            TextBox6.Text = ID
        End If

        If CheckBox1.Checked Then
            Dim Transactionxml As XElement = _
            <Transaction>
                <Dat id=<%= ID %>><%= Dat %></Dat>
                <Person id=<%= ID %>><%= Person %></Person>
                <BalanceI id=<%= ID %>><%= BalanceI %></BalanceI>
                <Price id=<%= ID %>><%= Price %></Price>
                <BalanceM id=<%= ID %>><%= BalanceM %></BalanceM>
                <Add id=<%= ID %>><%= Added %></Add>
                <Remove id=<%= ID %>><%= Removed %></Remove>
            </Transaction>

            Form1.RichTextBox1.Text = Form1.RichTextBox1.Text & Transactionxml.ToString & vbCr

        End If

        If CheckBox2.Checked Then

            Dim Transactionxaml As XElement = _
            <List FontFamily="Arial" FontSize="11pt">
                <ListItem><Paragraph>ID:<%= ID %></Paragraph></ListItem>
                <ListItem><Paragraph>Date of Transaction:<%= Dat %></Paragraph></ListItem>
                <ListItem><Paragraph>By whom:<%= Person %></Paragraph></ListItem>
                <ListItem><Paragraph>Balance (Items):<%= BalanceI %></Paragraph></ListItem>
                <ListItem><Paragraph>Price per Item:<%= Price %></Paragraph></ListItem>
                <ListItem><Paragraph>Balance (Money):<%= BalanceM %></Paragraph></ListItem>
                <ListItem><Paragraph>Amount Added:<%= Added %></Paragraph></ListItem>
                <ListItem><Paragraph>Amount Removed:<%= Removed %></Paragraph></ListItem>
            </List>

            Form1.RichTextBox2.Text = Form1.RichTextBox2.Text & Transactionxaml.ToString & vbCr
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        WriteData(TextBox6.Text)
    End Sub
    Private Sub LoadData()

        Try
            Me.Text = "Stock Taking Database" & " - " & Form1.TextBox3.Text

            Dim ID As String = TextBox6.Text

            Dim Stock As XElement = XElement.Load(Form1.TextBox7.Text & Form1.TextBox3.Text & ".xml")

            Dim Dat As XElement = _
                <Root>
                    <%= From el In Stock.<Transaction>.<Dat> _
                        Where el.Attribute("id") = ID _
                        Select el %>
                </Root>
            TextBox2.Text = Dat

            Dim Person As XElement = _
        <Root>
            <%= From el In Stock.<Transaction>.<Person> _
                Where el.Attribute("id") = ID _
                Select el %>
        </Root>
            TextBox3.Text = Person

            Dim BalanceI As XElement = _
        <Root>
            <%= From el In Stock.<Transaction>.<BalanceI> _
                Where el.Attribute("id") = ID _
                Select el %>
        </Root>
            TextBox4.Text = BalanceI

            Dim Price As XElement = _
       <Root>
           <%= From el In Stock.<Transaction>.<Price> _
               Where el.Attribute("id") = ID _
               Select el %>
       </Root>
            TextBox5.Text = Price

            Dim BalanceM As XElement = _
               <Root>
                   <%= From el In Stock.<Transaction>.<BalanceM> _
                       Where el.Attribute("id") = ID _
                       Select el %>
               </Root>
            TextBox9.Text = BalanceM

            Dim Added As XElement = _
            <Root>
                <%= From el In Stock.<Transaction>.<Add> _
                    Where el.Attribute("id") = ID _
                    Select el %>
            </Root>
            TextBox10.Text = Added

            Dim Removed As XElement = _
            <Root>
                <%= From el In Stock.<Transaction>.<Remove> _
                    Where el.Attribute("id") = ID _
                    Select el %>
            </Root>
            TextBox11.Text = Removed

            Dim Item As XElement = _
               <Root>
                   <%= From el In Stock.<Item> _
                       Select el %>
               </Root>
            TextBox1.Text = Item


            Dim Count As XElement = _
       <Root>
           <%= From el In Stock.<Counter>.<Count> _
               Select el %>
       </Root>

            Dim Counter As String = Count

            Label7.Text = "There are " & Counter & " record(s) in the XML file"
        Catch ex As Exception
            MsgBox("No file has been loaded.")

        End Try

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        LoadData()
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Try
            Do Until TextBox6.Text = TextBox8.Text
                CheckBox3.CheckState = CheckState.Unchecked
                Dim ID As Integer = TextBox6.Text
                ID = ID + 1
                TextBox6.Text = ID
                LoadData()
                WriteData(TextBox6.Text)
            Loop
        Catch ex As Exception
            MsgBox("Choose a proper range.")
        End Try

    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Nav.GoToFirst(TextBox6)

        LoadData()
    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        Nav.PrevRec(TextBox6)

        LoadData()
    End Sub

    Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
        Nav.NextRec(TextBox6)

        LoadData()
    End Sub

    Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click
        Nav.GoToLast(TextBox6, Form1.TextBox7.Text & Form1.TextBox3.Text & ".xml")
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        Try
            Dim Counter As Integer = TextBox6.Text

            Dim words As String = TextBox12.Text
            Dim split As String() = words.Split(New [Char]() {","c})

            Dim s As String
            For Each s In split
                If s.Trim() <> "" Then
                    TextBox6.Text = s
                    LoadData()
                    Counter = Counter + 1
                    WriteData(Counter)
                End If
            Next s

            TextBox6.Text = Counter

        Catch ex As Exception
            MsgBox("Extraction Failed")
        End Try

    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        Try
            Dim Counter As Integer = TextBox6.Text

            Dim words As String = TextBox13.Text
            Dim split As String() = words.Split(New [Char]() {"-"c})

            Dim Init As Integer = split(0) - 1
            Dim Fin As Integer = split(1)

            Do Until Init = Fin
                Init = Init + 1
                TextBox6.Text = Init
                LoadData()
                Counter = Counter + 1
                WriteData(Counter)
            Loop

            TextBox6.Text = Counter

        Catch ex As Exception
            MsgBox("Extraction Failed")
        End Try
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        Try
            Form13.Show()

            CheckBox3.CheckState = CheckState.Unchecked
            TextBox6.Text = 0

            LoadData()

            Dim ID As Integer = TextBox6.Text
            Dim XmlFile As XElement = XElement.Load(Form1.TextBox7.Text & Form1.TextBox3.Text & ".xml")

            Dim Counter As XElement = _
            <Root>
                <%= From el In XmlFile.<Counter>.<Count> _
                    Select el %>
            </Root>
            Dim Count As Integer = Counter

            Dim C As String = TextBox7.Text

            Dim Item As String = TextBox1.Text

            Dim Itemxml As XElement = _
            <Item><%= Item %></Item>

            Form13.RichTextBox1.Text = Form13.RichTextBox1.Text & "<Stock>" & vbCr _
                                      & "<Type>Stock Taking</Type>" & vbCr & Itemxml.ToString & vbCr

            Do Until ID = Count
                ID = ID + 1
                TextBox6.Text = ID

                LoadData()

                Dim Dat As String = TextBox2.Text
                Dim Person As String = TextBox3.Text
                Dim BalanceI As String = TextBox4.Text
                Dim Price As String = TextBox5.Text
                Dim BalanceM As String = TextBox9.Text
                Dim Added As String = TextBox10.Text
                Dim Removed As String = TextBox11.Text

                Dim Transactionxml As XElement = _
                    <Transaction id=<%= ID %>>
                        <Dat><%= Dat %></Dat>
                        <Person><%= Person %></Person>
                        <BalanceI><%= BalanceI %></BalanceI>
                        <Price><%= Price %></Price>
                        <BalanceM><%= BalanceM %></BalanceM>
                        <Add><%= Added %></Add>
                        <Remove><%= Removed %></Remove>
                    </Transaction>

                Form13.RichTextBox1.Text = Form13.RichTextBox1.Text & Transactionxml.ToString & vbCr

            Loop

            Dim LastRec As String = TextBox6.Text

            If CheckBox1.Checked Then
                Dim Counter2 As XElement = _
                <Counter>
                    <Count><%= LastRec %></Count>
                </Counter>

                Form13.RichTextBox1.Text = Form13.RichTextBox1.Text & Counter2.ToString & vbCr & "</Stock>"
            End If

        Catch ex As Exception
            MsgBox("Processing Failed!")
        End Try
    End Sub
End Class